package servlet;

import dao.ReviewDAO;
import pojo.Discuss;
import dao.DiscussDAO;
import pojo.Review;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/addDiscuss")
public class DiscussAddServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");

        String content = request.getParameter("content");
        String time = request.getParameter("time");
        int rw_id = Integer.parseInt(request.getParameter("rw_id"));
        int st_id = Integer.parseInt(request.getParameter("st_id"));
//        System.out.println(content + time + rw_id + st_id);

        Discuss discuss = new Discuss();

        discuss.setContent(content);
        discuss.setTime(time);
        discuss.setRw_id(rw_id);
        discuss.setSt_id(st_id);

        try {
            new DiscussDAO().add(discuss);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Review review = new ReviewDAO().getReviewById(rw_id);
        review.setDiscuss(review.getDiscuss()+1);
        try {
            new ReviewDAO().updateDiscuss(review, rw_id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String url = request.getParameter("url");
//        System.out.println(url);
        response.sendRedirect(url);

//        response.sendRedirect("discuss");
    }
}